
const express = require('express')
const fs = require('fs')
const path = require('path')
const app = express()


// 路由中间件
function accessLog(req, res, next) {
    if(req.query.code == 521){
        fs.appendFileSync(path.resolve(__dirname,'./access.log'),`${req.url} ${req.ip}\r\n`)
        next()
    }else{
        res.send('你不配')
    }
   
}

app.get('/home',accessLog , (req, res) => {
   res.send('前端首页')
})
app.get('/admin', accessLog , (req, res) => {
   res.send('后台首页')
 })
app.all('*',(req, res) => { 
    res.send('<h1>404 NOT FOUND</h1>')
 })
app.listen(3000, () => {
    console.log('服务器启动了')
})